/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.other.rest;

import com.qiaofang.applet.service.other.domain.AssetsType;
import com.qiaofang.applet.service.other.service.AssetsService;
import com.qiaofang.applet.service.other.service.dto.AssetsTypeDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @website https://el-admin.vip
* @author QF2019
* @date 2020-09-16
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "内容：资讯类型管理")
@RequestMapping("/api/assetsType")
public class AssetsTypeController {

    @Autowired
    private final AssetsService assetsService;

    @GetMapping
    @AnonymousAccess
    @ApiOperation("查询AssetsType")
    public ResponseEntity<Page<AssetsTypeDto>> query(Pageable pageable){
        AssetsType assetsType = new AssetsType();
        assetsType.setCorporationCode(SecurityUtils.getCorporationCode());
        List<AssetsTypeDto> assetsTypeDtos = assetsService.findAllType(assetsType.getCorporationCode());
        return ResponseEntity.ok(new PageImpl(assetsTypeDtos, pageable, assetsTypeDtos != null ? assetsTypeDtos.size() : 0));
    }

    @PostMapping
    @ApiOperation("新增AssetsType")
    public ResponseEntity<Object> create(@Validated @RequestBody AssetsType assetsType){
        assetsType.setCorporationCode(SecurityUtils.getCorporationCode());
        assetsService.saveAssetsType(assetsType);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @ApiOperation("修改AssetsType")
    public ResponseEntity<Object> update(@Validated @RequestBody AssetsType assetsType){
        assetsService.saveAssetsType(assetsType);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ApiOperation("删除AssetsType")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody List<String> ids) {
        if(!CollectionUtils.isEmpty(ids)){
            assetsService.removeAssetsType(ids.get(0));
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }
}